package com.lc.cehui.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.MyBookAddApi;
import com.lc.cehui.http.request.UpdateImageApi;
import com.lc.cehui.http.response.BaseBean;
import com.lc.cehui.http.response.UpdateImageBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.SPUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SubmitCertificateActivity extends AppActivity {
    @BindView(R.id.et_certificate_name)
    EditText etCertificateName;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.et_range)
    EditText etRange;
    @BindView(R.id.img_yingyezhizhao)
    ImageView imgYingyezhizhao;
    //输入框初始值
    private int num = 0;
    //输入框最大值
    public int mMaxNum = 120;
    private String picurl;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_submitcertificate;
    }

    @Override
    protected void initView() {
        etContent.addTextChangedListener(new TextWatcher() {
            //记录输入的字数
            private CharSequence wordNum;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //实时记录输入的字数
                wordNum = s;
            }

            @Override
            public void afterTextChanged(Editable s) {

                int number = num + s.length();
                //TextView显示剩余字数
                tvNum.setText("" + number + "/120");
                selectionStart = etContent.getSelectionStart();
                selectionEnd = etContent.getSelectionEnd();
                //判断大于最大值
                if (wordNum.length() > mMaxNum) {
                    //删除多余输入的字（不会显示出来）
//                        s.delete(selectionStart - 1, selectionEnd);
                    s.delete(mMaxNum, s.length());
                    int tempSelection = selectionEnd;
                    etContent.setText(s);
                    etContent.setSelection(tempSelection);//设置光标在最后
                    //吐司最多输入50字
                    toast("最多输入120字");
                }



            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_yingyezhizhao, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_yingyezhizhao:
                showAlbum(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.bt_submit:
                my_book_add();
                break;
        }
    }

    private void my_book_add() {
        EasyHttp.post(this)
                .api(new MyBookAddApi().setUserid(SPUtil.get(this, IntentKey.USERID,0)).setTitle(etCertificateName.getText().toString()).setContent(etContent.getText().toString()).setExhibition_range(etRange.getText().toString()).setPicurl(picurl))
                .request(new HttpCallback<BaseBean>(this) {

                    @Override
                    public void onSucceed(BaseBean data) {

                     if (callBackCode(data)){
                         finish();
                     }

                    }

                });
    }
    private void getCommon_uploads(File file) {
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImg(file))
                .request(new OnHttpListener<UpdateImageBean>() {

                    @Override
                    public void onSucceed(UpdateImageBean data) {
//
                        if (data.getCode().equals("200")) {

                                    GlideApp.with(SubmitCertificateActivity.this).load(IntentKey.PAGEURL + data.getData().getImg()).into(imgYingyezhizhao);
                                    picurl = data.getData().getImg();





                        }

                    }

                    @Override
                    public void onFail(Exception e) {

                    }
                });
    }
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:



                        List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                        getCommon_uploads(new File(selectList.get(0).getCutPath()));


                    break;
            }
        }
    }
}
